import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class MessageParser {
	
	public static String parse(byte[] packet)
	{
		int length = packet.length;
		String packetString = new String(packet);
		String type = null;
		String handshakePeerID;
		String index;
		String bitfield;
		String len;
		byte[] piece;
		
		if(packetString.substring(0, 3).equals("CNT"))
		{
			type = "handshaking";
			handshakePeerID = packetString.substring(28, length);
		}
		else if(packetString.substring(4,5).equals("0"))
		{
			// no payload
			type = "choke";
		}
		else if(packetString.substring(4,5).equals("1"))
		{
			// no payload
			type = "unchoke";
		}
		else if(packetString.substring(4,5).equals("2"))
		{
			// no payload
			type = "interested";
		}
		else if(packetString.substring(4,5).equals("3"))
		{
			// no payload
			type = "not interested";
		}
		else if(packetString.substring(4,5).equals("4"))
		{
			type = "have";
			index = packetString.substring(5,length);
		}
		else if(packetString.substring(4,5).equals("5"))
		{
			type = "bitfield";
			bitfield = packetString.substring(5,length);
		}
		else if(packetString.substring(4,5).equals("6"))
		{
			type = "request";
			index = packetString.substring(5,length);
		}
		else if(packetString.substring(4,5).equals("7"))
		{
			type = "piece";
			
			// get length field from packet
			len = packetString.substring(0,4);
			int msgLen = Integer.parseInt(len);
			
			System.out.println("msgLen: " + msgLen); 
			
			// get piece size
			ConfigReader cfg = new ConfigReader();
			cfg.readCommon();
			int pieceSize = cfg.pieceSize;
			String fileName = cfg.fileName;
			int fileSize = cfg.fileSize;
			int indexSize = length - 5 - pieceSize;
			
			// check to see if this is the last piece (the pieceSize will be less than expected)
			if(length < pieceSize+5)
			{
				System.out.println("must be last piece");
				
				// calculate actual piece size by computing the size of the index
				int numPieces = (fileSize + pieceSize - 1) / pieceSize;
				String numPiecesString = Integer.toString(numPieces);
				indexSize = numPiecesString.length(); 
				
				pieceSize = length - 5 - indexSize;
				
				System.out.println("actual piece size: " + pieceSize);
			}
			
			// put the file piece into piece byte array
			piece = new byte[pieceSize];
			index = packetString.substring(5,(5+indexSize));
			int j = 0;
			for(int i = 5+indexSize; i < length; i++)
			{
				piece[j] = packet[i];
				j++;
			}
			
			// write the piece to the properly named file
			String newFile = "peer_" + peerProcess.peerID + "/" + fileName + index;
	    	
	    	System.out.println("File part: " + newFile);
	    	
	    	FileOutputStream fos;
			try
			{
				fos = new FileOutputStream(newFile);
				fos.write(piece, 0, pieceSize);
				
				// flush and close the file output stream
		    	fos.flush();
		    	fos.close();
			} 
			catch (FileNotFoundException e)
			{
				e.printStackTrace();
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		return type;
	}
}
